package com.itheima.thread.utils.cyclicbarrier.demo01;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 一个线程类
 */
public class CyclicBarrierThread01 extends Thread {

    private CyclicBarrier cyclicBarrier ;
    public CyclicBarrierThread01(CyclicBarrier cyclicBarrier) {         // 构造方法的作用: 接收CyclicBarrier对象
        this.cyclicBarrier = cyclicBarrier ;
    }

    @Override
    public void run() {

        for(int x = 0;  x < 100 ; x++) {

            System.out.println("CyclicBarrierThread01....." + x);       // 输出

            try {

                // 让线程休眠3秒
                Thread.sleep(3000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(x == 20) {
                System.out.println("CyclicBarrierThread01线程已经到达了屏障点.....");
                try {
                    cyclicBarrier.await() ;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
